<?php
namespace Mob\Model;

use Think\Model;
use Think\Hook;
use Think\Model\AdvModel;
/**红包申请记录模型
 * Class InfoModel
 */
require_once('./Application/Mob/Common/function.php');
class RedPackRecordModel extends AdvModel
{
	protected $tableName = 'payment_record';  //指定默认表名
     /**
	 * creationRecord  创建记录
	 * @author:wanghongyan
	 */
	public function creationRecord($paySerialno,$paymenttype,$applyUid,$applyOpenId)
	{
	 $data['paySerialno']=$paySerialno;
	 $data['paymenttype']=$paymenttype;
	 $data['applyUid']=$applyUid;
	 $data['applyOpenId']=$applyOpenId;

	 $data['serial']=$this->getSerial($paySerialno);
	 $data['serialno']=getSerialno();
	 if($this->add($data))
	 {
	 return $data['serial'];
	 }

	}
	/**
	 * getSerial  取红包领取序号
	 * @author:wanghongyan
	 */
	public function getSerial($paySerialno,$isNext=true)
	{
	   $where = array('paySerialno'=>$paySerialno);
		$serial = $this->where($where)->getField('max(serial)');
		if($isNext)
		{
		return $serial+1;
		}else
		{
		if($serial)
		{
		return $serial;
		}else
		{
		return 0;
		}

		}

	}
	/**
	 * queryRecord  取红包领取记录
	 * @author:wanghongyan
	 */
	public function queryRecord($paySerialno)
	{
	 $where = array('paySerialno'=>$paySerialno);
    return $this->where($where)->field('paymenttype,applyTime,applyUid,applyOpenId,applyStatus,money,reason,serial,serialno')->find();

	}
	/**
	 * isApply  判断是否可以申请
	 * @author:wanghongyan
	 */
	public function isApply($paySerialno,$Uid)
	{
	if($Uid==0)
	{
	return false;
	}
	$where = array('paySerialno'=>$paySerialno,'applyUid'=>$Uid);
	$count = $this->where($where)->count();
	if($count==0)
	{
	return true;
	}else
	{
	return false;
	}

	}
	/**
	 * receiveRedPack  领取红包
	 * @author:wanghongyan
	 */


	public function receiveSucceed($paySerialno,$serial,$applyUid,$moeny,$redPackId,$applyOpenId)
	{
	$where = array('paySerialno'=>$paySerialno,'serial'=>$serial,'applyUid'=>$applyUid);
	$data['money']=$moeny;
	$data['applyStatus']=1;
	$data['redPackId']=$redPackId;
    $data['applyUid']=$applyUid;
    $data['applyOpenId']=$applyOpenId;
	$this->where($where)->save($data);

	}
	public function receiveFailure($paySerialno,$serial,$Uid,$reason='',$applyOpenId,$redPackId)
	{
    $where = array('paySerialno'=>$paySerialno,'serial'=>$serial,'applyUid'=>$Uid);
	$data['applyStatus']=2;
	$data['reason'] =  $reason;
	$data['applyOpenId']=$applyOpenId;
	$data['redPackId']=$redPackId;
    $this->where($where)->save($data);

	}
	public function  receiveRepeat($paySerialno,$serial,$Uid,$reason='')
	{
	$where = array('paySerialno'=>$paySerialno,'serial'=>$serial,'applyUid'=>$Uid);
	$data['applyStatus']=3;
	$data['reason'] =  $reason;
    $this->where($where)->save($data);

	}

	public function redPackRanking($paySerialno,$limit=5)
	{
	 $where = array('paySerialno'=>$paySerialno,'applyStatus'=>array('eq','1'));

   $res = $this->where($where)->field('FORMAT(money/100,2) as money,serial,unix_timestamp(applyTime) as applyTime,applyUid,applyStatus')->order('money desc')->limit($limit)->select();

   $count =1;
   foreach($res as &$v)
   {

   $v['rang']=$count;
   $count++;

   }
   unset($v);
   return $res;

	}
	//领取红包明细
    public function detailRecord($paySerialno)
    {
     $where = array('paySerialno'=>$paySerialno,'applyStatus'=>array('IN','1,2'));
     return  $this->where($where)->field('serial,unix_timestamp(applyTime),applyUid,applyStatus,FORMAT(money/100,2) as money')->order('applyTime,serial')->select();

    }
    public function getdetailConut($paySerialno)
    {
       $where = array('paySerialno'=>$paySerialno,'applyStatus'=>array('IN','1,2'));
       return $this->where($where)->count();



    }











}